% Interpolate data points with gaps less than N 
% source http://www.mathworks.com/matlabcentral/answers/87165
% uses an externally compiled RunLength.c mex file
% input x : data vector with NaN for gaps
% input maxgap : maximum gaps (-1) tolerated. above this number, the data
% will be left as it is. 

function x = shortinterpolate(x, maxgap)

% adding path to RunLength 
path(path,'/Users/manojnair/m/extern/RunLength_2013_09_17/');
[b, n]      = RunLength(isnan(x));
shortNaN    = RunLength(b' & (n < maxgap), n);
x(shortNaN) = interp1(find(~shortNaN), x(~shortNaN), find(shortNaN), 'linear');

